package com.training.controller;

import com.training.dto.CommentDisplayDto;
import com.training.entity.CommentReply;
import com.training.entity.Teacher; // You may need to import your Teacher entity
import com.training.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

// Use a base path for all methods in this controller related to teacher comments
@Controller
@RequestMapping("/teacher/comments")
public class CommentController {

    private final CommentService commentService;

    @Autowired
    public CommentController(CommentService commentService) {
        this.commentService = commentService;
    }

    /**
     * Handles GET requests to display the comment management page.
     * This method fetches comments and displays the 'comment-manage.html' view.
     */
    @GetMapping("/manage")
    public String showCommentManagePage(Model model) {
        // !!! IMPORTANT !!!
        // You need a way to get the ID of the currently logged-in teacher.
        // This is usually done via a security framework like Spring Security or Shiro.
        // For demonstration, I am using a hardcoded ID of 1.
        // PLEASE REPLACE THIS with your actual logic to get the teacher's ID.
        Long currentTeacherId = 1L; // <-- REPLACE WITH YOUR AUTHENTICATION LOGIC

        List<CommentDisplayDto> comments = commentService.getCommentsByTeacherId(currentTeacherId);
        model.addAttribute("comments", comments);

        // This tells Spring to render the "comment-manage.html" template
        return "comment-manage";
    }

    /**
     * Handles POST requests from the reply form.
     * This method saves the teacher's reply and redirects back to the management page.
     */
    @PostMapping("/reply")
    public String handleReply(CommentReply reply, RedirectAttributes redirectAttributes) {
        // !!! IMPORTANT !!!
        // You also need the user ID of the currently logged-in teacher.
        // Your Teacher entity should be linked to a User entity.
        // For demonstration, I am using hardcoded IDs.
        // PLEASE REPLACE THIS with your actual logic.
        Long currentUserId = 1L;   // <-- REPLACE: Get the USER ID associated with the teacher
        Long currentTeacherId = 1L; // <-- REPLACE: Get the TEACHER ID

        try {
            // Set the teacher ID on the reply object before saving
            reply.setTeacherId(currentTeacherId);

            // Call the service method with the reply object AND the user ID
            commentService.addReply(reply, currentUserId);

            // Add a success message to be displayed after redirecting
            redirectAttributes.addFlashAttribute("successMessage", "回复成功！");
        } catch (Exception e) {
            // Add an error message if something goes wrong
            redirectAttributes.addFlashAttribute("errorMessage", "回复失败: " + e.getMessage());
        }

        // Redirect to the management page to prevent duplicate form submissions on refresh
        return "redirect:/teacher/comments/manage";
    }
}